import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:s_mankey/config/net/smonkey_ui_api.dart';
import 'package:s_mankey/config/resource_mananger.dart';
import 'package:s_mankey/config/router_config.dart';
import 'package:s_mankey/generated/I18n.dart';
import 'package:s_mankey/provider/provider_widget.dart';
import 'package:s_mankey/service/smonkey_repository.dart';
import 'package:s_mankey/ui/widget/home_car_widget.dart';
import 'package:s_mankey/ui/widget/show_toast.dart';
import 'package:s_mankey/view_model/home_vmodel.dart';

//ArticleModel model;

class ArticleList extends StatefulWidget {

  var datas;

  ArticleList({this.datas});

  @override
  State<StatefulWidget> createState() {
    return _ArticleList();
  }
}

class _ArticleList extends State<ArticleList>
    with SingleTickerProviderStateMixin {

  ValueNotifier<int> valueNotifier = ValueNotifier(0);
  TabController _tabController;

//  @override
//  bool get wantKeepAlive => true; //切换页面不会重载

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: 1, vsync: this); //这里的长度要跟分类的长度一样.
  }


  @override
  Widget build(BuildContext context) {
//  print('##datas${widget.datas.length}');
    return HomeCardWidget(
      child: Container(
        margin: EdgeInsets.all(AppPadding.Padding10),
        child: Column(
          children: <Widget>[
            _setTabSort(tabController: this._tabController, context: context,),
            Container(
              height: 600,
              child: TabBarView(
                controller: this._tabController,
                children: <Widget>[
                  ListView.separated(
                      shrinkWrap: true,
                      physics: NeverScrollableScrollPhysics(),
                      //禁止滚动
                      itemBuilder: (BuildContext context, int index) {
                        return _setImageDetail(index: index, context: context, datas: widget.datas);
                      },
                      separatorBuilder: (BuildContext context, int index) {
                        return Divider(color: Colors.grey); // 分割线
                      },
                      itemCount: widget.datas.length),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

///  课程分类
Widget _setTabSort({TabController tabController, BuildContext context}) {
  return Row(
    mainAxisAlignment: MainAxisAlignment.spaceBetween,
    children: <Widget>[
      Container(
        width: 90,
        height: 30,
        child: DefaultTabController(
          length: 1,
          child: Container(
            child: TabBar(
                controller: tabController,
                labelColor: Color(0xffff3d7c),
                unselectedLabelColor: Colors.white,
                indicator: BoxDecoration(
                    border: Border.all(color: Color(0xffff3d7c)),
                    borderRadius: BorderRadius.all(Radius.circular(20))),
                tabs: [
                  Tab(
                    child: Text(
                      I18n.current.allCourses,
                      style: TextStyle(fontSize: AppFontSize.Size14),
                      textAlign: TextAlign.center,
                    ),
                  ),
                ]),
          ),
        ),
      ),
//      GestureDetector(
//        child: Text(
//          '更多',
//          style: TextStyle(fontSize: AppFontSize.Size16, color:Color(0xffff3d7c)),
//        ),
//        onTap: () {
//          Toast.toast(context,msg: "功能暂未开放 ");
////          Navigator.of(context).pushNamed(RouterName.showDialogDemo);
//        },
//      ),
    ],
  );
}

/// 课程的图片和文字描述
Widget _setImageDetail({BuildContext context, int index, List datas}) {
  return GestureDetector(
      onTap: () {
        print('跳到了第$index个详情页');

      },
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          ClipRRect(
            borderRadius:
            BorderRadius.all(Radius.circular(AppPadding.Padding5)),
            child: Image.network(
            datas[index]['type_photo_link'],
              fit: BoxFit.fill,
              width: 83,
              height: 103,
            ),
          ),
          Expanded(
            child: Container(
              height: 140,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Padding(
                    padding: EdgeInsets.fromLTRB(10, 0, 10, 10),
                    child: Text(
                      datas[index]['course_name'],
//                    'course_name',
                      overflow: TextOverflow.clip,
                      softWrap: true,
                      maxLines: 1,
                      style: TextStyle(fontSize: AppFontSize.Size16, color: Colors.black),
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.only(left: AppPadding.Padding10),
                    child: Text(
                      datas[index]['type_short_desc'],
//                    'type_short_desc',
                      overflow: TextOverflow.clip,
                      softWrap: true,
                      maxLines: 3,
                      style: TextStyle(fontSize: AppFontSize.Size14, color: Color(0xff747474)),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );

}
